package binaryTree.ClosestCommonAncestor;

import binaryTree.TreeNode;

import java.util.HashSet;

public class Class1676 {
   public   TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes){
       HashSet<Integer> set = new HashSet<>();
       for (TreeNode node: nodes
            ) {
           set.add(node.val);

       }
       return  find(root,set);
    }

    public  TreeNode  find(TreeNode root,HashSet<Integer> set){
       if (root==null){
           return null;
       }
       if (set.contains(root.val)){
           return  root;
       }
        TreeNode left = find(root.left, set);
        TreeNode right = find(root.right, set);

        if (left!=null&&right!=null){
            return root;
        }
        return  left!=null?left:right;

    }
}
